package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.editpolicies.DirectEditPolicy;

import edu.pku.sei.modeler.editpolicies.UMLDiagramGraphicalNodeEditPolicy;
import edu.pku.sei.modeler.figures.EntrypointPseudostateFigure;
import edu.pku.sei.modeler.figures.ExitpointPseudostateFigure;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.structure.StructurePackage;
import edu.pku.sei.modeler.model.uml.BehaviorStateMachines.Pseudostate;

public class AdsorbPseudostateEditpart extends AdsorbNodeEditpart {
	private int width;
	private int height;

	public AdsorbPseudostateEditpart(MetaShapeElement model) {
		super(model);
		width = 20;
		height = 20;
		offsetX = -10;
		offsetY = -10;
	}

	// @Override
	// protected MetaNode getFatherMetaNode() {
	// if (fatherNode != null)
	// return fatherNode;
	//		
	// MetaNode node = getNode();
	// Pseudostate p = (Pseudostate) node.getModelElement();
	// List nodes;
	// if (node.getParent() == null) {
	// nodes = node.getDiagram().getNodes();
	// } else {
	// nodes = node.getParent().getNodes();
	// }
	// for (int i = 0; i < nodes.size(); i++) {
	// if (((MetaNode)nodes.get(i)).getModelElement() == p.getContainer())
	// return (MetaNode) nodes.get(i);
	// }
	// return null;
	// }

	@Override
	protected DirectEditPolicy createDirectEditPolicy() {
		// TODO Auto-generated method stub
		return null;
	}

	public Pseudostate getModelElement() {
		return (Pseudostate) ((MetaNode) getModel()).getModelElement();
	}

	protected IFigure createFigure() {
		Pseudostate pseudostate = this.getModelElement();
		if (pseudostate.getKind().getValue() == 7) {
			pseudostate.setName("EntryPoint");
			return new EntrypointPseudostateFigure();
		} else if (pseudostate.getKind().getValue() == 8) {
			pseudostate.setName("ExitPoint");
			return new ExitpointPseudostateFigure();
		}
		return null;
	}

	protected void createEditPolicies() {
		super.createEditPolicies();
	}

	protected void refreshVisuals() {
		Rectangle constraint = new Rectangle(0, 0, -1, -1);
		MetaNode node = getNode();
		Point p;
		if (adsorbed) {
			p = calculateLocation(node.getLocationX(), node.getLocationY());
			node.setLocationX(p.x);
			node.setLocationY(p.y);
		} else {
			p = new Point(node.getLocationX(), node.getLocationY());
		}
		constraint.setLocation(p);
		constraint.width = width;
		constraint.height = height;
		((GraphicalEditPart) getParent()).setLayoutConstraint(this,
				getFigure(), constraint);
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		if (StructurePackage.NODE_HEIGHT.equals(propertyName)) {

		} else if (StructurePackage.NODE_WIDTH.equals(propertyName)) {

		} else if (StructurePackage.NODE_LOCATIONX.equals(propertyName)) {
			refreshVisuals();
		} else if (StructurePackage.NODE_LOCATIONY.equals(propertyName)) {
			refreshVisuals();
		} else {
			super.handlePropertyChanged(event);
		}
	}

}
